/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package queryDB;

import java.util.ArrayList;
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 *
 * @author Paulo
 */
@WebService()
public class QueryDatabase {
   private PlaceJpaController place;

   private PlaceJpaController getController()
    {
        if ( place==null)
            this.place=new PlaceJpaController();

        return place;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "insertPlace")
    public Boolean insertPlace(@WebParam(name = "name")
    String name, @WebParam(name = "lat")
    String lat, @WebParam(name = "lng")
    String lng, @WebParam(name = "address")
    String address, @WebParam(name = "details")
    String details, @WebParam(name = "price")
    String price, @WebParam(name = "schedule")
    String schedule, @WebParam(name = "contact1")
    String contact1, @WebParam(name = "contact2")
    String contact2, @WebParam(name = "type")
    String type, @WebParam(name = "image")
    String image) {
        
        long id = -1;
        double l1, l2;
        Place p=new Place();

        if (name != null){
            p.setName(name);
            id = getController().getNextID();
            id++;
            p.setId(id);
            if (address != null && p.addressValid(address))
                   p.setAddress(address);
                try{
                    l1 = Double.parseDouble(lat);
                    if (Double.isNaN(l1))
                        p.setLat(l1);
                }
                catch(Exception e){
                }
                try{
                    l2 = Double.parseDouble(lng);
                    if (Double.isNaN(l2))
                        p.setLat(l2);
                }
                catch(Exception e){
                }

                if (details != null && p.detailsValid(details))
                   p.setDetails(details);
                if (price != null && p.priceValid(price))
                   p.setPrice(price.charAt(0));
                if (contact1 != null && p.phoneNumberValid(contact1))
                   p.setContact1(Integer.parseInt(contact1));
                if (contact2 != null && p.phoneNumberValid(contact2))
                   p.setContact1(Integer.parseInt(contact2));
                if (image != null && image.length() > 0)
                   p.setImage(image);
                if (type != null && p.typeValid(type))
                   p.setType(type);

                getController().create(p);
                return true;
        }
        else{
            System.out.println("Não foi possível inserir o lugar na base de dados.");
            return false;
        }
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "updatePlace")
    public Boolean updatePlace(@WebParam(name = "name")
    String name, @WebParam(name = "lat")
    String lat, @WebParam(name = "lng")
    String lng, @WebParam(name = "address")
    String address, @WebParam(name = "details")
    String details, @WebParam(name = "price")
    String price, @WebParam(name = "schedule")
    String schedule, @WebParam(name = "contact1")
    String contact1, @WebParam(name = "contact2")
    String contact2, @WebParam(name = "type")
    String type, @WebParam(name = "image")
    String image) {

        long id = -1;
        double l1 = 0.0, l2 = 0.0;
        Place p;

        if (name.length() > 0){
            id = getController().getID(name);
            if (id != -1){
                p = getController().findPlace(id);
                p.setId(id);
                if (address != null && p.addressValid(address))
                   p.setAddress(address);
                try{
                    l1 = Double.parseDouble(lat);
                    if (Double.isNaN(l1))
                        p.setLat(l1);
                }
                catch(Exception e){
                }
                try{
                    l2 = Double.parseDouble(lng);
                    if (Double.isNaN(l2))
                        p.setLat(l2);
                }
                catch(Exception e){
                }
                
                if (details != null && p.detailsValid(details))
                   p.setDetails(details);
                if (price != null && p.priceValid(price))
                   p.setPrice(price.charAt(0));
                if (contact1 != null && p.phoneNumberValid(contact1))
                   p.setContact1(Integer.parseInt(contact1));
                if (contact2 != null && p.phoneNumberValid(contact2))
                   p.setContact1(Integer.parseInt(contact2));
                if (image != null && image.length() > 0)
                   p.setImage(image);
                if (type != null && p.typeValid(type))
                   p.setType(type);
                p.setFavorite("false");
                p.setUpdated("true");
                getController().edit(p);
                return true;
            }
            else
                System.out.println("Não foi possivel actualizar o lugar: "+name);
                return false;
        }
        else
            return false;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "deletePlace")
    public Boolean deletePlace(@WebParam(name = "name")
    String name) {

        long id = -1;
        if (name.length() > 0){
            id = getController().getID(name);
            if (id != -1)
                getController().destroy(id);
            return true;
        }
        else
            return false;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "downloadBD")
    public List<String> downloadDB() {
        List<Place> p = new ArrayList<Place>();
        List<String> l = new ArrayList<String>();
        Place t = new Place();
        String str = new String();

        p = getController().findPlaceEntities();

        for(int i=0; i < getController().getPlaceCount(); i++){
            t = p.get(i);
            str = t.placeToString();
            l.add(str);
        }
        return l;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "votePlace")
    public Boolean votePlace(@WebParam(name = "name")
    String name, @WebParam(name = "rate") String rate) {
        Place p = new Place();
        long id = -1;
        int g=0, b=0;
        String r="";
        String[] array;

        try{
            id = getController().getID(name);
            p = getController().findPlace(id);
            r = p.getRate();
            array = r.split("/");
            g = Integer.parseInt(array[0]);
            b = Integer.parseInt(array[1]);

            if (rate.equals("good"))
                g++;
            else if (rate.equals("bad"))
                b++;

            rate = g + "/" + b;
            p.setRate(rate);

            getController().edit(p);

            return true;
        }
        catch(Exception e){
            return false;
        }
    }
}
